// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { game } from "../../../../script/game/control/game";
import { ccs } from "../../../../script/util/services";
import { WinBase } from "../../../../script/wins/WinBase";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends WinBase {

    @property(cc.Label)
    label: cc.Label = null;

    minMoney: number = 20000;
    maxMoney: number = 1000000;
    money: number = 10000;
    count: number = 6; //交换次数
    canChoose: boolean = false; //是否可以选
    poker: string = "6";
    pokerArr: string[] = ['K', "A", "6", "2"];
    multiple: number = 1; //下注的倍数
    speed: number = 0.25;
    times: number = 6;
    start () {
        this.addBtn();
        let info = ccs.dataMgr.getGambleListById(this._args.type);
        this.minMoney = info.money;
        this.maxMoney = info.max;
    }
    setSpeed(count) {
        if(count == 1) {
            this.speed = 0.25;
        } else {
            this.speed = 0.5 / count;
        }
        this.times = 8 * count;
        this.count = this.times;
    }
    addBtn() {
        this.touchEndEventScale(this.nodes.ensureBuy, () => {
            if(this.money <= 0 || this.money * this.multiple < this.minMoney ) {
                ccs.winMgr.showWin("toast", {msg: "购买不得低于" + this.minMoney + "元"});
                return ;
            }else if(this.maxMoney < this.money * this.multiple) {
                ccs.winMgr.showWin("toast", {msg: "此玩法上限为" + this.maxMoney + "元"});
            }
            let type = "guess";
            if(this._args.type == "guess") {
                type = "gamblePokerWin";
            } else {
                type = "gambleSizeWin";
            }
            //生成牌号
            let res = game.playerMgr.calMoney(-this.money * this.multiple);
            if(!res) return;
            game.gameMgr.saveGame();
            this._close();
            ccs.winMgr.showWin(type, {
                speed: this.speed,
                times: this.times,
                count: this.count,
                money: this.money * this.multiple, //钱
            });
            game.playerMgr.achieveFinish('gamble', 1);
            this.nodes.note.getComponent(cc.Label).string = `请老板不要眨眼`;
            
        })
        this.touchEndEventScale(this.nodes.add, ( ) => {
            this.money += 10000;
            this.limitCount();
        })
        this.touchEndEventScale(this.nodes.reduce, ( ) => {
            this.money -= 10000;
            this.limitCount();
        })
        this.touchEndEventScale(this.nodes.one, ( ) => {
            this.multiple = 1;
            this.setSpeed(1);
            this.limitCount();
        })
        this.touchEndEventScale(this.nodes.two, ( ) => {
            this.multiple = 2;
            this.setSpeed(2);
            this.limitCount();
        })
        this.touchEndEventScale(this.nodes.five, ( ) => {
            this.multiple = 5;
            this.setSpeed(3);
            this.limitCount();
        })
        this.touchEndEventScale(this.nodes.ten, ( ) => {
            this.multiple = 10;
            this.setSpeed(4);
            this.limitCount();
        })
        this.touchEndEventScale(this.nodes.twenty, ( ) => {
            this.multiple = 20;
            this.setSpeed(5);
            this.limitCount();
        })
        
    }
    onSlider(tar, i) {
        let num = tar.progress;
        let tmp = Math.ceil(num * game.playerMgr.heroInfo.money);
        this.money = tmp;
        this.limitCount();
    }
    limitCount() {
        if(this.money < 0) {
            this.money = 0;
        }else if(this.money > game.playerMgr.heroInfo.money) {
            this.money = game.playerMgr.heroInfo.money;
        }
        this.nodes.money.getComponent(cc.Label).string = ccs.gf.getOmitNum(this.money);
        this.nodes.multipleNum.getComponent(cc.Label).string = ccs.gf.getOmitNum(this.multiple);
        this.nodes.maxNum.getComponent(cc.Label).string = ccs.gf.getOmitNum(this.money * this.multiple);

    }
}
